package org.weasel.controllers;

import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.weasel.tree.TreeObjectRelease;
import org.weasel.views.ReleaseDetail;

public class ReleaseViewController {
	private static ReleaseViewController instance = null;
	private ApplicationController appController;
	private TreeObjectRelease to_release;
	private ReleaseDetail releaseView;
	
	public static ReleaseViewController instance() {
		if(instance == null)
			instance = new ReleaseViewController();
		return instance;
	}
	
	private ReleaseViewController() {
	}
	
	public void setApplicationController(ApplicationController appController){
		this.appController = appController;
	}

	public void setTo_release(TreeObjectRelease to_release) {
		this.to_release = to_release;
	}

	public void showReleaseView() {
		try {
//			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(ReleaseDetail.ID, to_release.getRelease().getId(),IWorkbenchPage.VIEW_ACTIVATE);
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(ReleaseDetail.ID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
//		IViewReference ref =  page.findViewReference(ReleaseDetail.ID,to_release.getRelease().getId());
//		IViewPart viewPart = ref.getView(false);
//		releaseView = (ReleaseDetail) viewPart;
		releaseView = (ReleaseDetail) page.findView(ReleaseDetail.ID);
		releaseView.showReleaseInformation(to_release.getRelease());		
	}

	public void setReleaseName(String text) {
		to_release.getRelease().setName(text);
		to_release.setName(text);
		appController.refreshTreeViewer();
		appController.changesOccured();
	}

	public void setNotes(String text) {
		to_release.getRelease().setNotes(text);
		appController.changesOccured();
	}

	public void updateReleaseViews() {
		if(releaseView!=null)
			releaseView.showReleaseInformation(to_release.getRelease());
	}
}
